package com.adb.receivecontent.uc;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.adb.receivecontent.R;
import com.adb.receivecontent.bean.LinkParam;

public class AddParamsAdapter extends BaseAdapter implements OnClickListener {

	private Context mCntext;
	private LayoutInflater mLayInflater;
	public ArrayList<LinkParam> mAdditionalParams;
	public IMenuClickListener mClickCallback;
	
	public AddParamsAdapter(Context mContext) {
		mCntext = mContext;
		mLayInflater = LayoutInflater.from(this.mCntext);
		mAdditionalParams = new ArrayList<LinkParam>();
		mClickCallback = (IMenuClickListener)mContext;
	}

	public void addParmasItems(LinkParam mTempParams) {

		if (mAdditionalParams != null) {
			mAdditionalParams.add(mTempParams);
		}
		notifyDataSetChanged();

	}
	
	public void removeItems(int location){
		
		if(mAdditionalParams != null){
			
			mAdditionalParams.remove(location);
			notifyDataSetChanged();
		}
	}
	
	public void replaceItems(int location,LinkParam mTempParams){
		if(mAdditionalParams != null){
			mAdditionalParams.set(location,mTempParams);
		}
		notifyDataSetChanged();
	}
	
	public void addParamsLists(ArrayList<LinkParam> mArrayListParams){
		
		if(!mAdditionalParams.isEmpty()){
			mAdditionalParams.clear();
		}
		for(LinkParam mTempBean : mArrayListParams){
			mAdditionalParams.add(mTempBean);
		}
		notifyDataSetChanged();
	}
	
	public LinkParam getLocationParams(int location) {
		LinkParam mLocationParams = null;
		if (mAdditionalParams != null && mAdditionalParams.size() > 0) {
			mLocationParams = mAdditionalParams.get(location);
		}
		return mLocationParams;
	}
	
	public ArrayList<LinkParam> getCurrentListofLinkParams(){
		
		if(mAdditionalParams != null && mAdditionalParams.size() > 0){
			return mAdditionalParams;	
		}
		return null;
		
	}
	
	@Override
	public int getCount() {
		return mAdditionalParams.size();
	}

	@Override
	public Object getItem(int position) {
		return mAdditionalParams.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		CatchHolderView mViewHolder;
		
		if(convertView == null){
			mViewHolder = new CatchHolderView();
			
			convertView = mLayInflater.inflate(R.layout.row_additional_items,parent, false);
			
			mViewHolder.mtextView_key = (TextView)convertView.findViewById(R.id.textView_key);
			mViewHolder.mtextView_value = (TextView)convertView.findViewById(R.id.textView_value);
			mViewHolder.mImageView_edit_params = (ImageView)convertView.findViewById(R.id.imageview_edit_params);
			mViewHolder.mImageView_edit_params.setTag(position);
			mViewHolder.mImageView_delete_params = (ImageView)convertView.findViewById(R.id.imagview_delete_params);
			mViewHolder.mImageView_delete_params.setTag(position);
			
			
			convertView.setTag(mViewHolder);
			
		}else{
			
			mViewHolder = (CatchHolderView)convertView.getTag();
		}
		
		LinkParam mItemLinkParam;
		
		if(mAdditionalParams != null){
			
			mItemLinkParam = mAdditionalParams.get(position);
			
			mViewHolder.mtextView_key.setText(mItemLinkParam.getStrKey());
			mViewHolder.mtextView_value.setText(mItemLinkParam.getStrValue());
			mViewHolder.mImageView_edit_params.setOnClickListener(this);
			mViewHolder.mImageView_delete_params.setOnClickListener(this);
			
		}
		
		return convertView;
	}
	
	class CatchHolderView{
		
		TextView mtextView_key;
		TextView mtextView_value;
		ImageView mImageView_edit_params;
		ImageView mImageView_delete_params;
	}

	@Override
	public void onClick(View v) {
		
		if(v.getId() == R.id.imageview_edit_params){
			mClickCallback.onLinkMenuClick(v.getId(), v.getTag());
		}else if(v.getId() == R.id.imagview_delete_params){
			mClickCallback.onLinkMenuClick(v.getId(),v.getTag());
		}
	
	}

}
